package com.roadrunner;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

public class CurrentJobsActivity extends FragmentActivity implements
		listOrderFragment.OnHeadlineSelectedListener {

	JsonController 					mJC = null;
	private static ArrayList<Job>	m_JobsList = null;
	listOrderFragment firstFragment = null;
	
	private String sPrefTag = "JobList";
	private String sServer = null;
	private String sTransporter = null;
	private String sActivityName = "CurrentJobsActivity";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_jobs);

		// get the server address:
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		try {
			sServer = prefs.getString("CurrentServer", "");
			sTransporter = prefs.getString("CurrentUser", "");
			
		} catch (Exception e) {
		    e.printStackTrace();
		}
				
		if (findViewById(R.id.fragment_container) != null) {
			if (savedInstanceState != null)
				return;

			// Create an instance of ExampleFragment
			firstFragment = new listOrderFragment();
			getIntent().putExtra("FROM", sActivityName);
			firstFragment.setArguments(getIntent().getExtras());

			// Add the fragment to the 'fragment_container' FrameLayout
			getSupportFragmentManager().beginTransaction()
					.add(R.id.fragment_container, firstFragment).commit();
		}
		
		MyHttp task = new MyHttp(this, sPrefTag);
		//task.execute(new String[] { sServer + "view", "transporterName,"+ });
		
		try {
		    String serverResponse = prefs.getString(sPrefTag, "");
		    
		    mJC = new JsonController();
		    m_JobsList =  mJC.parseOrders(serverResponse);
		} catch (Exception e) {
		    e.printStackTrace();
		}
	}

	public void onArticleSelected(int position) {
		// The user selected an order from the orders list

		// Capture the article fragment from the activity layout
		OrderFragment orderFragment = (OrderFragment) getSupportFragmentManager()
				.findFragmentById(R.id.orderDetails);

		if (orderFragment != null) {
			// If article frag is available, we're in two-pane layout... and we
			// have to update the fragment
			orderFragment.updateArticleView(position);

		} else {
			// If the frag is not available, we're in the one-pane layout and
			// must swap frags...
			OrderFragment newFragment = new OrderFragment();
			Bundle args = new Bundle();
			args.putInt(OrderFragment.ARG_POSITION, position);
			args.putString("FROM", sActivityName);
			newFragment.setArguments(args);
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();

			// Replace whatever is in the fragment_container view with this
			// fragment,
			// and add the transaction to the back stack so the user can
			// navigate back
			transaction.replace(R.id.fragment_container, newFragment);
			transaction.addToBackStack(null);

			// Commit the transaction
			transaction.commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.home:
			finish();
			// startActivity(new Intent(this, MainActivity.class));
			break;

		case R.id.my_jobs:
			break;

		case R.id.jobs_list: {
			startActivity(new Intent(this, JobsList.class));
			finish();
		}
			break;

		case R.id.action_settings:
			break;

		case R.id.exit_app:
			System.exit(0);
			break;

		default:
			break;

		}

		return super.onOptionsItemSelected(item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}
	
	public static ArrayList<Job> getJobList()
	{
		return m_JobsList;
	}
}
